﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SAM_Edu.control;
using SAM_Edu.entity;
using SAM_Edu.form;

namespace SAM_Edu.form
{
    public partial  class frmLogon : Form
    {
     
        public frmLogon()
        {
            InitializeComponent();
        }

        public ulong getUserRights(string username)
        {
           ulong value= ManageFeatures.requestUserRights(username);
           return value;
         
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (tbusername.Text == "")
            {
                MessageBox.Show("Username is empty");
                return;
            }
            if (tbPassword.Text == "")
            {
                MessageBox.Show("Password is empty");
                return;
            }
            
            // Receive menu settings but for now just true false
           Accounts AccountDetails = Logon.AuthenticateUser(tbusername.Text, tbPassword.Text);

         

            if (Logon.verifyLogonDetails == true) // Success
            {
                MessageBox.Show("Login Success");
               Accounts acc = new Accounts(tbusername.Text);
               User user = new User(Logon.getuserid);
               string username = acc.Username;
             ulong val=   getUserRights(username);
               frmMain m = new frmMain(acc,user);

               Form frm = (Form)this.MdiParent;
               MenuStrip ms = (MenuStrip)frm.Controls["SAMMenu"];
               ToolStripMenuItem ti = (ToolStripMenuItem)ms.Items["userToolStripMenuItem"];
               ti.DropDownItems["createUserToolStripMenuItem"].Enabled = Convert.ToBoolean((val & 0x0000000000001) != 0);
               ti.DropDownItems["updateUsersToolStripMenuItem"].Enabled = Convert.ToBoolean((val & 0x0000000000002) != 0);
               ti.DropDownItems["manageUserToolStripMenuItem"].Enabled = Convert.ToBoolean((val & 0x0000000000004) != 0);
               m.Refresh();
               this.Close();
            }
            else //Failed
            {
                MessageBox.Show("wrong username or password");
            }
        }

        private void tbPassword_TextChanged(object sender, EventArgs e)
        {

        }

        private void tbusername_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void lblTitle_Click(object sender, EventArgs e)
        {

        }
    }
}
